package com.yingjie.yingjiegateway.filter;


import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class RequestLogGlobalFilter implements GlobalFilter, Ordered {

    //路由地址匹配器，用于检查是否包含我们设置的不需要过滤的路由
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
    /**
     * @param exchange 网络交换机
     * @param chain    过滤器链
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 获取请求路径
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String rul = request.getPath().toString();
        System.out.println(rul);

        return chain.filter(exchange);
    }

    /**
     *
     * @param urls 不需要过滤的路由
     * @param requestUrl 本次请求的路由地址
     * @return
     */
    public boolean check(String[] urls, String requestUrl) {
        for (String url : urls) {
            if (PATH_MATCHER.match(url, requestUrl)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 设置过滤器执行顺序，数值越低，优先级越高
     *
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}